/**
* Determine if a given integer IPv4 address is in a given CIDR network
+ * @deprecated Use wfIsAddressInRange
*/
function isAddressInRange( $addr, $range ) {
- list( $network, $bits ) = wfParseCIDR( $range );
- if ( $network !== false && $addr >> ( 32 - $bits ) == $network >> ( 32 - $bits ) ) {
- return true;
- } else {
- return false;
- }
+ return wfIsAddressInRange( $addr, $range );
}
function initFromRow( $row )
{
$this->mRangeStart = '';
$this->mRangeEnd = '';
+
if ( $this->mUser == 0 ) {
- list( $network, $bits ) = wfParseCIDR( $this->mAddress );
- if ( $network !== false ) {
- $this->mRangeStart = sprintf( '%08X', $network );
- $this->mRangeEnd = sprintf( '%08X', $network + (1 << (32 - $bits)) - 1 );
- }
+ $startend = wfRangeStartEnd($this->mAddress);
+ $this->mRangeStart = $startend[0];
+ $this->mRangeEnd = $startend[1];
}
}
# Don't collide with expired blocks
Block::purgeExpired();
-
+
$ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
$dbw->insert( 'ipblocks',
array(
/**
* Autoblocks the given IP, referring to this Block.
- *@param $autoblockip The IP to autoblock.
- *@return Whether or not an autoblock was inserted.
+ * @param $autoblockip The IP to autoblock.
+ * @return bool Whether or not an autoblock was inserted.
*/
function doAutoblock( $autoblockip ) {
# Check if this IP address is already blocked
$dbw =& wfGetDb( DB_MASTER );
$dbw->begin();
+ # If autoblocks are disabled, go away.
if ( !$this->mEnableAutoblock ) {
return;
}
+ # Check for presence on the autoblock whitelist
+ # TODO cache this?
+ $lines = explode( "\n", wfMsgForContentNoTrans( 'autoblock_whitelist' ) );
+
+ $ip = wfGetIp();
+
+ wfDebug("Checking the autoblock whitelist..\n");
+
+ foreach( $lines as $line ) {
+ # List items only
+ if ( substr( $line, 0, 1 ) !== '*' ) {
+ continue;
+ }
+
+ $wlEntry = substr($line, 1);
+ $wlEntry = trim($wlEntry);
+
+ wfDebug("Checking $wlEntry\n");
+
+ # Is the IP in this range?
+ if (wfIsAddressInRange( $ip, $wlEntry )) {
+ wfDebug("IP $ip matches $wlEntry, not autoblocking\n");
+ #$autoblockip = null; # Don't autoblock a whitelisted IP.
+ return; #This /SHOULD/ introduce a dummy block - but
+ # I don't know a safe way to do so. -werdna
+ }
+ }
+
+ # It's okay to autoblock. Go ahead and create/insert the block.
+
$ipblock = Block::newFromDB( $autoblockip );
if ( $ipblock ) {
# If the user is already blocked. Then check if the autoblock would
}
}
+/**
+ * Get the start and end of a range.
+ * @param $range The range to get the start and end for.
+ * @return array An array with the first element as the start of the range, as a long, and the second element as the end of the range, also as a long.
+ *
+ */
+function wfRangeStartEnd( $range ) {
+ list( $network, $bits ) = wfParseCIDR( $range );
+ if ( $network !== false ) {
+ $start = sprintf( '%08X', $network );
+ $end = sprintf( '%08X', $network + (1 << (32 - $bits)) - 1 );
+ return array($start, $end);
+ }
+ return false;
+}
+
+/**
+ * Determine if a given integer IPv4 address is in a given CIDR network
+ * @param $addr The address to check against the given range.
+ * @param $range The range to check the given address against.
+ * @return bool Whether or not the given address is in the given range.
+ */
+function wfIsAddressInRange( $addr, $range ) {
+ $unsignedIP = IP::toUnsigned($addr);
+ $startend = wfRangeStartEnd($range);
+ $start = $startend[0];
+ $end = $startend[1];
+
+ return (($unsignedIP >= $start) && ($unsignedip <= $end));
+}
+
/*
* Get a Database object
* @param integer $db Index of the connection to get. May be DB_MASTER for the
$ret =& $wgLoadBalancer->getConnection( $db, true, $groups );
return $ret;
}
-
-
?>